package eu.sudparis.it.opp.demo;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintStream;
import java.util.HashMap;
import java.util.List;
import java.util.Set;

import eu.sudparis.it.opp.Node;
import eu.sudparis.it.opp.TimeSeries;
import eu.sudparis.it.opp.ex.SocialTags;
import eu.sudparis.it.opp.observfram.*;

public class DNode extends Node {
	public static TimeSeries ts;

	public SocialTags self = null;
	public static String path = "E://mobile_dataset/Exp6/forms/";
	public static HashMap<Double, Long> simvspause = new HashMap<Double, Long>();
	public static HashMap<Double, Long> max = new HashMap<Double, Long>();
	public static HashMap<Double, Long> min = new HashMap<Double, Long>();
	public static HashMap<Double, Integer> times = new HashMap<Double, Integer>();
	public static PrintStream ps = null;
	static {
		try {
			ps = new PrintStream("E://infocom06//pause_topic_filtered_75m.dat");
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void initial() {
		String profile_path = path + this.id + ".dat";
		self = new SocialTags(new File(profile_path));
		System.out.println(self.profile);
	}

	@Override
	public void encounter(Node nd) {
		if (self == null) {
			this.initial();
		}
		if (((DNode) (nd.chain_of_responsibility)).self == null) {
			((DNode) nd.chain_of_responsibility).initial();
		}
	}

	@Override
	public void leaveWith(Node nd) {
		double sim = this.self.cosineSimilarityWith(
				((DNode) (nd.chain_of_responsibility)).self, "topics");
		List<Serie> ls = ((ObserverNode) nd).local.series.get(this.id);
		Serie last = ls.get(ls.size() - 1);
		long pause = ts.currentTime - last.start;
		if (!simvspause.containsKey(sim)) {
			simvspause.put(sim, pause);
			times.put(sim, 1);
			max.put(sim, pause);
			min.put(sim, pause);
		} else {
			simvspause.put(sim, simvspause.get(sim) + pause);
			times.put(sim, times.get(sim) + 1);
			if(pause>max.get(sim))
				max.put(sim, pause);
			if(pause<min.get(sim))
				min.put(sim, pause);
		}
		// ps.println(format.format(sim) + "\t" + pause);

		// TODO Auto-generated method stub
		// System.out.println(this.id+" leaves "+nd.id+"\t@:"+ts.getCurrentTime());
	}

	public DNode clone() {
		return new DNode();
	}

	@Override
	public void communicate(Set<Node> nods) {
		// TODO Auto-generated method stub

	}
}